package com.yq.bdm.controller.common;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.entity.UserInfoEntity;
import com.yq.bdm.service.UserInfoService;
import com.yq.bdm.utils.MD5Util;
import com.yq.bdm.utils.ResultMap;

/**
 * 登录
 * 
 * @author yuq
 * 
 * @date 2017年05月19日 下午9:05:27
 */
@Controller
@RequestMapping("/login")
public class LoginController extends AbstractController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	@RequestMapping("/")
	public String login() {
		//获取session里的登录状态值
		Object isLogin = request.getSession().getAttribute("isLogin");
	    //如果登录状态不为空则返回true，返回true则会执行相应controller的方法
	    if(isLogin != null){
	    	return "default/lockscreen.html";
	    }
		return "default/login.html";
	}
	
	/**
	 * 登录验证
	 * @param email
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loginVerification")
	public ResultMap loginVerification(String email, String password) {
		boolean isSuccess = false;
		Map<String, Object> map = new HashMap<>();
		map.put("email", email);
		try {
			map.put("password", MD5Util.md5Encode(password));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info("密码加密失败!");
		}
		UserInfoEntity userInfo = userInfoService.queryUser(map);
		if(userInfo != null){
			session.setAttribute("isLogin", 0);
			session.setAttribute("userInfo", userInfo);
			isSuccess = true;
		}
		//namespaceUrl参数传递给前台做跳转处理
		return ResultMap.ok().put("certification", isSuccess);
	}
}
